/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufg.inf.si.poo.formula1.GUI;

import br.ufg.inf.si.poo.formula1.Circuit.Circuit;
import br.ufg.inf.si.poo.formula1.Interface.InterfaceRegister;
import br.ufg.inf.si.poo.formula1.Library.Library;
import br.ufg.inf.si.poo.formula1.ManagementFiles.ManagementFiles;
import java.text.ParseException;
import javax.swing.JOptionPane;

/**
 * RegisterCircuit
 *
 * Versao 1.0
 *
 * Data 16/02/2013
 *
 * @author Igor
 */
public class RegisterCircuit extends javax.swing.JInternalFrame implements InterfaceRegister {
    //Atributos
    private Circuit circuit;
    private Boolean emEdicao; //Valida se esta em modo de edicao ou nao
    private Boolean novo; //Variavel que verifica se clicou no botao de novo registro

    //Construtores
    /**
     * Creates new form FramePadrao
     */
    public RegisterCircuit() {
        initComponents();
        this.novo = false;
        clearFields();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelTitulo = new javax.swing.JPanel();
        labelTitulo = new javax.swing.JLabel();
        panelDadosPrincipais = new javax.swing.JPanel();
        labelNome = new javax.swing.JLabel();
        labelPrimeiroGrandPrix = new javax.swing.JLabel();
        labelNumeroVoltas = new javax.swing.JLabel();
        labelTamanho = new javax.swing.JLabel();
        labelDistanciaVolta = new javax.swing.JLabel();
        labelVoltaRecord = new javax.swing.JLabel();
        labelPais = new javax.swing.JLabel();
        labelCidade = new javax.swing.JLabel();
        textNome = new javax.swing.JTextField();
        textCidade = new javax.swing.JTextField();
        textPais = new javax.swing.JTextField();
        textNumeroVoltas = new javax.swing.JTextField();
        labelData = new javax.swing.JLabel();
        textDitancia = new javax.swing.JTextField();
        textTamanho = new javax.swing.JTextField();
        panelCircuitosCadastrados = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listCircuitos = new javax.swing.JList();
        labelCampoCodigo = new javax.swing.JLabel();
        labelPrimeiroGrandPrix1 = new javax.swing.JLabel();
        labelVoltaRecord1 = new javax.swing.JLabel();
        labelData1 = new javax.swing.JLabel();
        panelBotoes = new javax.swing.JPanel();
        buttonSalvar = new javax.swing.JButton();
        buttonLimpar = new javax.swing.JButton();
        buttonNovo = new javax.swing.JButton();
        labelCamposObrigatorios = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("Cadastro de Circuitos");
        setPreferredSize(new java.awt.Dimension(800, 500));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        panelTitulo.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        labelTitulo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        labelTitulo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        labelTitulo.setText("Tela utilizada para cadastrar e/ou alterar dados do Circuito (Independente da Temporada)");
        labelTitulo.setToolTipText("");

        javax.swing.GroupLayout panelTituloLayout = new javax.swing.GroupLayout(panelTitulo);
        panelTitulo.setLayout(panelTituloLayout);
        panelTituloLayout.setHorizontalGroup(
            panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTitulo)
                .addContainerGap(105, Short.MAX_VALUE))
        );
        panelTituloLayout.setVerticalGroup(
            panelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTitulo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelDadosPrincipais.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "   Dados Principais   ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 10), java.awt.Color.black)); // NOI18N

        labelNome.setText("Nome do Circuito:");

        labelPrimeiroGrandPrix.setText("Primeiro Grand Prix:");

        labelNumeroVoltas.setText("Número de Voltas: *");

        labelTamanho.setText("Tamanho (KM):");

        labelDistanciaVolta.setText("Distância da Volta (KM):");

        labelVoltaRecord.setText("Volta recorde:");

        labelPais.setText("País: *");

        labelCidade.setText("Cidade:");

        labelData.setText("Data da Corrida:");

        panelCircuitosCadastrados.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "   Circuitos   ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 10), java.awt.Color.black)); // NOI18N

        listCircuitos.setToolTipText("Lista de circuitos cadastrados");
        listCircuitos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listCircuitosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listCircuitos);

        javax.swing.GroupLayout panelCircuitosCadastradosLayout = new javax.swing.GroupLayout(panelCircuitosCadastrados);
        panelCircuitosCadastrados.setLayout(panelCircuitosCadastradosLayout);
        panelCircuitosCadastradosLayout.setHorizontalGroup(
            panelCircuitosCadastradosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
        );
        panelCircuitosCadastradosLayout.setVerticalGroup(
            panelCircuitosCadastradosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );

        labelCampoCodigo.setText("Campo código");
        labelCampoCodigo.setToolTipText("Não editar");

        labelPrimeiroGrandPrix1.setText("Valores");

        labelVoltaRecord1.setText("Valores");

        labelData1.setText("Valores");

        javax.swing.GroupLayout panelDadosPrincipaisLayout = new javax.swing.GroupLayout(panelDadosPrincipais);
        panelDadosPrincipais.setLayout(panelDadosPrincipaisLayout);
        panelDadosPrincipaisLayout.setHorizontalGroup(
            panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDadosPrincipaisLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelVoltaRecord)
                    .addComponent(labelPrimeiroGrandPrix)
                    .addComponent(labelTamanho)
                    .addComponent(labelDistanciaVolta)
                    .addComponent(labelNumeroVoltas)
                    .addComponent(labelNome)
                    .addComponent(labelData)
                    .addComponent(labelCidade)
                    .addComponent(labelPais))
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelData1)
                    .addComponent(textCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textPais, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textNumeroVoltas, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textDitancia, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textTamanho, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPrimeiroGrandPrix1)
                    .addComponent(labelVoltaRecord1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelCampoCodigo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
                .addComponent(panelCircuitosCadastrados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        panelDadosPrincipaisLayout.setVerticalGroup(
            panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDadosPrincipaisLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(labelPais)
                    .addComponent(textPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCampoCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCidade))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelData)
                    .addComponent(labelData1))
                .addGap(11, 11, 11)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNumeroVoltas)
                    .addComponent(textNumeroVoltas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textDitancia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDistanciaVolta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(labelTamanho)
                    .addComponent(textTamanho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(labelPrimeiroGrandPrix)
                    .addComponent(labelPrimeiroGrandPrix1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDadosPrincipaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(labelVoltaRecord)
                    .addComponent(labelVoltaRecord1))
                .addContainerGap(97, Short.MAX_VALUE))
            .addComponent(panelCircuitosCadastrados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        panelBotoes.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        buttonSalvar.setText("Salvar");
        buttonSalvar.setToolTipText("Clique para salvar os dados digitados");
        buttonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSalvarActionPerformed(evt);
            }
        });

        buttonLimpar.setText("Limpar");
        buttonLimpar.setToolTipText("Clique para limpar a tela e habilitar um novo registro");
        buttonLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLimparActionPerformed(evt);
            }
        });

        buttonNovo.setText("Novo");
        buttonNovo.setToolTipText("Clique para cadastrar um novo registro");
        buttonNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNovoActionPerformed(evt);
            }
        });

        labelCamposObrigatorios.setText("Campos marcados com *, são obrigatórios");

        javax.swing.GroupLayout panelBotoesLayout = new javax.swing.GroupLayout(panelBotoes);
        panelBotoes.setLayout(panelBotoesLayout);
        panelBotoesLayout.setHorizontalGroup(
            panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelCamposObrigatorios)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buttonNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonLimpar)
                .addContainerGap())
        );

        panelBotoesLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonLimpar, buttonNovo, buttonSalvar});

        panelBotoesLayout.setVerticalGroup(
            panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(buttonNovo)
                    .addComponent(buttonSalvar)
                    .addComponent(buttonLimpar)
                    .addComponent(labelCamposObrigatorios))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelDadosPrincipais, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelBotoes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelDadosPrincipais, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void listCircuitosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listCircuitosValueChanged
        // TODO add your handling code here:
        if(this.listCircuitos.getSelectedIndex() != -1){
            Circuit circuit1 = Library.returnCircuit( (String)this.listCircuitos.getSelectedValue());
            if(circuit1 != null){
                this.circuit = circuit1;
                this.emEdicao = true;
            }
        }
        dataShowObject();
    }//GEN-LAST:event_listCircuitosValueChanged

    private void buttonLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLimparActionPerformed
        // TODO add your handling code here:
        clearFields();
    }//GEN-LAST:event_buttonLimparActionPerformed

    private void buttonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSalvarActionPerformed
        // TODO add your handling code here:
        try{
            checkFields();

            checkValueEntered();

            createObject();

            clearFields();
        }catch(IllegalArgumentException e){
            JOptionPane.showMessageDialog(this, e.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
        }catch(ParseException e){
            JOptionPane.showMessageDialog(this, e.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_buttonSalvarActionPerformed

    private void buttonNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNovoActionPerformed
        // TODO add your handling code here:
        this.novo = true; //Diz para o clearFields() que sera um novo registro
        clearFields();
    }//GEN-LAST:event_buttonNovoActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:
        clearFields();
    }//GEN-LAST:event_formComponentShown

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonLimpar;
    private javax.swing.JButton buttonNovo;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCampoCodigo;
    private javax.swing.JLabel labelCamposObrigatorios;
    private javax.swing.JLabel labelCidade;
    private javax.swing.JLabel labelData;
    private javax.swing.JLabel labelData1;
    private javax.swing.JLabel labelDistanciaVolta;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelNumeroVoltas;
    private javax.swing.JLabel labelPais;
    private javax.swing.JLabel labelPrimeiroGrandPrix;
    private javax.swing.JLabel labelPrimeiroGrandPrix1;
    private javax.swing.JLabel labelTamanho;
    private javax.swing.JLabel labelTitulo;
    private javax.swing.JLabel labelVoltaRecord;
    private javax.swing.JLabel labelVoltaRecord1;
    private javax.swing.JList listCircuitos;
    private javax.swing.JPanel panelBotoes;
    private javax.swing.JPanel panelCircuitosCadastrados;
    private javax.swing.JPanel panelDadosPrincipais;
    private javax.swing.JPanel panelTitulo;
    private javax.swing.JTextField textCidade;
    private javax.swing.JTextField textDitancia;
    private javax.swing.JTextField textNome;
    private javax.swing.JTextField textNumeroVoltas;
    private javax.swing.JTextField textPais;
    private javax.swing.JTextField textTamanho;
    // End of variables declaration//GEN-END:variables

    //Metodos reescritos da interface InterfaceRegister
    @Override
    public void dataShowObject() {
        this.textPais.setText(this.circuit.getCountry());
        this.textCidade.setText(this.circuit.getCity());
        this.textNome.setText(this.circuit.getCircuitName());
        this.textNumeroVoltas.setText(""+this.circuit.getNumberLaps());
        this.textDitancia.setText(""+this.circuit.getCircuitLength());
        this.textTamanho.setText(""+this.circuit.getRaceDistance());

        this.labelData1.setText(this.circuit.getRaceDate());
        this.labelPrimeiroGrandPrix1.setText(""+this.circuit.getFirstGrandPrix());
        this.labelVoltaRecord1.setText(Library.convertMilliseconds(this.circuit.getLapRecordTime(),"MINUTOS")+" - "+this.circuit.getLapRecordDriver());

        Library.indexedListCircuits(this.listCircuitos, FrameMain.getCircuits(), null);
    }

    @Override
    public void checkFields() throws IllegalArgumentException {
        if(this.textPais.getText().equals("")
        || this.textNumeroVoltas.getText().equals("")) {
            throw new IllegalArgumentException("Campos obrigatórios estão vazios.");
        }
    }
    
    @Override
    public void checkValueEntered() throws IllegalArgumentException, ParseException {
        //Numero de Voltas
        try{
            Integer.parseInt(this.textNumeroVoltas.getText());
        }catch(NumberFormatException e){
            throw new IllegalArgumentException("Valor inválido digitado para o campo 'Número de Voltas'!");
        }

        //Distancia de Voltas
        try{
            Double.parseDouble(this.textDitancia.getText());
        }catch(NumberFormatException e){
            throw new IllegalArgumentException("Valor inválido digitado para o campo 'Distância da Volta (KM)'!");
        }

        //Tamanho do Circuito
        try{
            Double.parseDouble(this.textTamanho.getText());
        }catch(NumberFormatException e){
            throw new IllegalArgumentException("Valor inválido digitado para 'Tamanho (KM)'!");
        }
    }

    @Override
    public void createObject() throws IllegalArgumentException {
        //Se nao estiver em modo de edicao entao adiciona, pois eh um novo Objeto, caso contrario apenas alterara
        if (!this.emEdicao) {
            if(Library.returnCircuit(this.textPais.getText()) != null){
                throw new IllegalArgumentException("Circuito com 'País' "+this.textPais.getText()+" já cadastrado.");
            }
            this.circuit.setCountry(this.textPais.getText());
            FrameMain.setCircuits(this.circuit);
        }

        this.circuit.setCity(this.textCidade.getText());
        this.circuit.setCircuitName(this.textNome.getText());
        this.circuit.setNumberLaps(Integer.parseInt(this.textNumeroVoltas.getText()));
        this.circuit.setCircuitLength(Double.parseDouble(this.textDitancia.getText()));
        this.circuit.setRaceDistance(Double.parseDouble(this.textTamanho.getText()));

        ManagementFiles.writeCircuit(FrameMain.getCircuits());
    }

    @Override
    public void clearFields(){
        this.circuit = new Circuit();
        this.emEdicao = false;

        if(!this.novo){
            this.listCircuitos.setSelectedIndex(0);
        }
        this.novo = false;

        dataShowObject();
        Library.indexedListCircuits(this.listCircuitos, FrameMain.getCircuits(), null);
    }
}
